<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class AdController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Ad");
		$this->cate = D("AdCat");
		$this->types=array("code"=>"代码","img"=>"单图","imgs"=>"多图","html"=>"HTML");
		
	}
	
	function index(){
	
		$categorys=$this->cate->select();
		
		$this->assign("list",$categorys);
		
		
		$this->display();
	}
	
	/**
	 *  添加分类
	 */
	public function add_cate() {
		if (IS_POST) {
			if ($this->cate->create($_POST)) {
				if ($this->cate->add()!==false) {
					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->cate->getError());
			}
		}else{
		
		$this->display("cate");
		}
	}
	
	function edit_cate(){

		if (IS_POST) {
			if ($this->cate->create()) {
				if ($this->cate->save()!==false) {
					$this->success("保存成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->cate->getError());
			}
		}
		else{
		$id= intval(I("get.id"));
		$cate=$this->cate->where("id=$id")->find();
		$this->assign("data",$cate);
		$this->assign("edit",true);
		$this->display("cate");
		}
	}
	
	function db_list(){
		$catid=I("get.catid");
		if(!$catid){
			$this->error("缺少参数！");
		}
		$list=$this->db->where(array("catid"=>$catid))->order(array("listorder" => "DESC","id"=>"DESC"))->select();
		
		$cate=$this->cate->where(array("id"=>$catid))->find();
		$this->assign("catname",$cate[name]);
		$this->assign("type",$cate[type]);
		
		
		$this->assign("list",$list);
		$this->display();
		
		
		
		
	}
	
	function add(){
		
		if(IS_POST){
			if ($this->db->create($_POST)) {
				if ($this->db->add()!==false) {
					$this->success("添加成功！", U("db_list",array("catid"=>$_POST['catid'])));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{
		$catid=I("get.catid");
		
		$type=$this->cate->where(array("id"=>$catid))->getField("type");
		$this->assign("type_name",$this->types[$type]);
		
		$this->assign("cates",$this->_get_cate_tree($catid));
		$this->assign("catid",$catid);
		$this->display($type);
		}
		}
	
	
	function edit(){
		
		
		if(IS_POST){
			if ($this->db->create()) {
				
				if ($this->db->save()!==false) {
					$this->success("保存成功！", U("db_list",array("catid"=>$_POST['catid'])));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}
	
		
		$r=$this->db->where("id=$id")->find();
		
		$type=$this->cate->where(array("id"=>$r[catid]))->getField("type");
		$this->assign("type_name",$this->types[$type]);
		
		
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->assign("catid",$r[catid]);
		$this->assign("cates",$this->_get_cate_tree($r[catid]));
		$this->display($type);
		}
	}
	

	
	

	function delete(){
		
			$id = intval(I("get.id"));
	
			$rs=$this->db->where(array("id"=>$id))->getField("thumb");
			if ($this->db->delete($id)) {
				
				if(rs){
				D('File')->del_file($rs);
				}
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}


	}
	
	function delete_cate(){
	
			$id = intval(I("get.id"));
			
			if(!$id){
				$this->error("缺少参数！");
			}
	
			$count=$this->db->where(array("catid"=>$id))->find();

			if($count){
				$this->error("该栏目下还有数据，删除失败");
			}
			
			
			if ($this->cate->delete($id)) {
	
	
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		
	}
	
	

	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	public function _get_cate_tree($value){
		
		$list=$this->cate->select();

		if($value){
			foreach ($list as $key=>$v){
				
				if($value==$v[id]) {
					$list[$key]['selected']="selected";
					
				}
				
			}
			
		}
		return $list;
		
	}
	
	}